package a66weding.com.jiehuntong.http;

import android.app.ActivityManager;
import android.content.Context;
import android.util.Log;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;

import a66weding.com.jiehuntong.application.GlobalApplication;

/**
 * Created by Administrator on 2016/4/3.
 * 对Volley的封装
 */
public class VolleyManager {

    //请求队列
    private static RequestQueue mRequestQueue;
    private static ImageLoader mImageLoader;

    /**
     * 得到消息队列
     * @return
     */
    public static RequestQueue getRequestQueue(Context context){
        GlobalApplication applicationContext = (GlobalApplication) context.getApplicationContext();
        mRequestQueue= applicationContext.getRequestQueueFirst(context);

        return mRequestQueue;
    }

    /**
     * @return
     */
    public static ImageLoader getImageLoader(Context context){
        int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
        if(mImageLoader==null) {
            int memClass = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE))
                    .getMemoryClass();
            int cacheSize = 1024 * 1024 * memClass / 8;
        }
            return mImageLoader;
    }

    /**
     * 把请求添加到队列中
     * @param request
     * @param tag
     */
    public static void addRequestQueue(Request<?> request, Object tag){
        if(tag!=null) {
            request.setTag(tag);
        }
        if(mRequestQueue!=null && request!=null) {
            mRequestQueue.add(request);
        }
    }

    /**
     * 取消所有请求
     * @param tag
     */
    public static void cancelAll(Object tag) {
        if(mRequestQueue!=null) {
            mRequestQueue.cancelAll(tag);
        }
    }



}
